﻿package	birdry.ui	{
	
	import birdry.manager.TweenManager;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import gs.TweenLite;
	import birdry.utils.ArrayUtil;
	
	public class RotationList extends EventDispatcher	{
		
		public static const RENDER:String = "render";
		public static const MOVE_COMPLETE:String = "move_complete";
		
		var i:uint;
		var divisionRadian:Number;
		
		var _position:Number = 0;
		var total:uint;
		
		public var radiusX:uint = 200;
		public var radiusY:uint = 200;
		public var direct:String = "left";
		public var circleArr:Array;
		public var saveNum:int = 0;
		public var startNum:uint = 0;
		
		
		public function RotationList(_total:uint): void	{
			this.total = _total;
			this.divisionRadian = 2 * Math.PI / total;
			circleArr = [];
		}
		
		
		public function sort(_postion:Number = 0): void	{
			
			var _order:uint;
			var _x:Number;
			var _y:Number;
			var _rotation:Number;
			var _sideNum:uint;
			
			for (i = 0; i < total; ++i)		{
				_order = (direct == "left") ? i : total - i;
				_x = radiusX * Math.cos(divisionRadian * ((_order + startNum) - (total-_postion)));
				_y = radiusY * Math.sin(divisionRadian * ((_order + startNum) - (total-_postion)));
				_rotation = (Math.atan2(0-_y, 0-_x)*(180/Math.PI))-90;
				
				_sideNum = (i + Math.round(total-_postion)) % total;
				if (_sideNum > Math.floor(total / 2))	_sideNum = total - _sideNum;
				
				circleArr[i] = { x:_x, y:_y, rotation:_rotation, sideNum:_sideNum, order:_order, position: _position };
				
				/*
				var xx:Number = ( Math.abs(i - position) < Math.floor(total / 2 )) ? i - position : total - Math.abs(i-position);
				var scale:Number =  1 - Math.abs(xx) * 0.08;
				//circleArr[i].txt.text = xx;
				if (scale < 0.2)	scale = 0;
				circleArr[i].scaleX = scale;
				circleArr[i].scaleY = scale;
				
				container.addChildAt(circleArr[i], total - (sideNum * 2));
				*/
			}
			dispatchEvent( new Event( RotationList.RENDER ));
		}
		
		
		public function move(num:int): void	{
			saveNum += num;
			if (saveNum < 0)	{
				saveNum = total - 1;
			}	else if ( saveNum > total - 1)	{
				saveNum = 0;
			}
			moveTo(saveNum)
		}
		
		public function moveTo(num:int): void	{
			position = position % total;
			var order:uint  = (direct == "left") ?  total - num : num;
			if ( Math.abs(position - order) > total / 2)	{
				position = (position - order > 0)	?	position -= total	:	position += total;
			}
			
			var delta:Number = Math.abs(saveNum - order);
			var speed:Number =  (delta < Math.floor(total / 2)) ? 0.3 + ( delta * 0.1) : 0.3 + ( (total - delta) * 0.1);
			saveNum = order;
			
			TweenLite.to(this, speed, { position:order, ease:TweenManager.Cubic_easeOut, onComplete: function()	{
				dispatchEvent( new Event( RotationList.MOVE_COMPLETE ));
			}} );
		}
		
		public function get position(): Number	{
			return _position;
		}
		
		public function set position( value: Number ): void	{
			_position = value;
			sort(_position);
		}
	}	// class
}	// package